
package com.aat.dxfy.spring.nocfgfile.webmvc;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * @author jiangt
 */
@Configuration
@ComponentScan(basePackages = "com.sense.developer")
@ImportResource("classpath:properties-config.xml")
@EnableTransactionManagement(proxyTargetClass = true)
public class ContextConfig {

	@Bean
	public DataSource dataSource() {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setUrl(jdbcUrl);
		dataSource.setUsername(userName);
		dataSource.setPassword(password);
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setPoolPreparedStatements(true);
		dataSource.setInitialSize(10);
		dataSource.setMinIdle(Integer.valueOf(minConnect));
		return dataSource;
	}

	@Bean
	public SqlSessionFactoryBean sessionFactory(DataSource dataSource) {
		SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(dataSource);
		sessionFactory.setTypeAliasesPackage("com.aat.dizena.base.bean,com.aat.dizena.chat.bean ");
		return sessionFactory;
	}

	public MapperScannerConfigurer mapperScannerConfigurer(String sessionFactory) {
		MapperScannerConfigurer mapscan = new MapperScannerConfigurer();
		mapscan.setBasePackage("com.aat.dizena.**.dao");
		mapscan.setSqlSessionFactoryBeanName(sessionFactory);
		return mapscan;
	}

	@Bean
	public DataSourceTransactionManager transactionManager(DataSource dataSource) {
		DataSourceTransactionManager manager = new DataSourceTransactionManager();
		manager.setDataSource(dataSource);
		return manager;
	}

	@Bean
	public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {
		BeanNameAutoProxyCreator creator = new BeanNameAutoProxyCreator();
		creator.setBeanNames("*Controller");
		creator.setInterceptorNames("behaviorLogger");
		return creator;
	}

	@Bean
	public CommonsMultipartResolver multipartResolver() {
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
		multipartResolver.setMaxUploadSize(104857600);
		return multipartResolver;
	}

	@Value("${mysql.driver.url}")
	private String jdbcUrl;

	@Value("${mysql.user.name}")
	private String userName;

	@Value("${mysql.user.password}")
	private String password;

	@Value("${mysql.connect.max}")
	private String maxConnect;

	@Value("${mysql.connect.min}")
	private String minConnect;

	@Value("${hibernate.show_sql}")
	private String hibernateShowSql;
}
